<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品信息编辑--${site.name}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="description" content="${site.description}"/>
    <meta name="keywords" content="${site.keywords}"/>
    <meta name="author" content="${site.author}"/>
    <link rel="icon" href="${site.logo}">
    <link rel="stylesheet" href="${base}/static/layui/css/layui.css" media="all" />
    <style type="text/css">
        .layui-form-item .layui-inline{ width:33.333%; float:left; margin-right:0; }
        @media(max-width:1240px){
            .layui-form-item .layui-inline{ width:100%; float:none; }
        }
        .layui-form-item .role-box {
            position: relative;
        }
        .layui-form-item .role-box .jq-role-inline {
            height: 100%;
            overflow: auto;
        }
		.layui-upload-img-container{
        	position: relative;
        }
        .layui-upload-img {
		    width: 92px;
		    height: 92px;
		    margin: 10px;
		}
		.layui-upload-img-del{
			position: absolute;
			right: 0;
			top: -50px;
			font-size:24px;
		}
		.layui-table-cell, .layui-table-tool-panel li {
		     overflow: auto; 
		    /* text-overflow: ellipsis; */
		    white-space: normal; 
		}
    </style>
</head>
<body class="childrenBody">
<form class="layui-form" >
    <input value="${mallPSku.id}" name="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  货品</label>
        <div class="layui-input-block">
			<input  type="hidden" readonly  class="layui-input" value="${mallPSku.spuId}" id="spuId" name="spuId" lay-verify="required" >
            <input  type="text" readonly ts-selected="${mallPSku.spuId}" value="${spuName}" class="layui-input"  id="spuName" name="spuName" lay-verify="required" placeholder="请选择货品">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  SKU编号</label>
        <div class="layui-input-block">
                <input  type="text" class="layui-input" value = "${mallPSku.skuno}" name="skuno" lay-verify="required" placeholder="请输入SKU编号">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  SKU名称</label>
        <div class="layui-input-block">
                <input  type="text" class="layui-input" value = "${mallPSku.name}" name="name" lay-verify="required" placeholder="请输入SKU名称">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span> 成本价</label>
        <div class="layui-input-inline">
                <input  type="number" class="layui-input" lay-verify="required"  value = "${mallPSku.priceCost}" name="priceCost"  placeholder="请输入成本价">
        </div>
        <div class="layui-form-mid layui-word-aux">元</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  销售价</label>
        <div class="layui-input-inline">
                <input  type="number" class="layui-input" value = "${mallPSku.priceSale}" name="priceSale" lay-verify="required" placeholder="请输入销售价">
        </div>
        <div class="layui-form-mid layui-word-aux">元</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> 划线价格</label>
        <div class="layui-input-inline">

            <input  type="number"  class="layui-input" name="priceDel" value = "${mallPSku.priceDel}" placeholder="请输入划线价格">
        </div>
        <div class="layui-form-mid layui-word-aux">元</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  推荐</label>
        <div class="layui-input-inline">
			<input type="checkbox" <#if mallPSku.isrec>checked</#if> name="isrec" lay-skin="switch" lay-filter="switchTest" lay-text="是|否">
        </div>
    </div>
     <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  属性</label>
        <div class="layui-input-block">
            <div id="attrName" class="xm-select-demo"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服务保障</label>
        <div class="layui-input-block">
            <div id="serviceName" class="xm-select-demo"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  主图</label>
        <div class="layui-input-block">
                <input type="hidden" class="layui-input" name="thumbnail" id="thumbnail" value="${mallPSku.thumbnail}" >
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test_thumbnail">上传主图</button>
                    <div class="layui-upload-list">
                        <img  style="width:80px;height:80px;"  class="layui-upload-img" id="demo_thumbnail" <#if (mallPSku.thumbnail??)>src="${mallPSku.thumbnail}"</#if> >
                        <p id="demoText_thumbnail"></p>
                    </div>
                </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"> <span class="required">*</span>  介绍图</label>
        <div class="layui-input-block">

            <input type="hidden" class="layui-input" name="imgs" id="imgs" >
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test_imgs">上传介绍图</button>
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
				    <div class="layui-upload-list" id="demo2"></div>
				 </blockquote>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addMallPSku">立即提交</button>
             <button class="layui-btn layui-btn-primary" onclick="parent.layer.close(parent.layer.getFrameIndex(window.name)); ">关闭</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="${base}/static/layui/layui.js"></script>
<script type="text/javascript" src="${base}/static/layui/xm-select.js"></script>
<script>
    layui.config({
		base : "${base}/static/layui/lay/mymodules/"
	}).use(['form','jquery','layer','upload','tableSelect'],function(){
        var form      = layui.form,
                $     = layui.jquery,
                layer = layui.layer;

      //商品属性
        var xmSelectInstanceGoods;
        $.get('${base}${adminPrefix}/mall-pattr/xmSelectData',{skuId:'${mallPSku.id}'},function(ajaxdata){
        	//console.log(ajaxdata)
        	xmSelectInstanceGoods = xmSelect.render({
	        	el: '#attrName', 
	        	autoRow: true,
	        	filterable: true,
	        	tree: {
	        		show: false,
	        		showFolderIcon: true,
	        		showLine: true,
	        		indent: 20,
	        		expandedKeys: [ -3 ],
	        	},
	        	toolbar: {
	        		show: true,
	        		list: ['ALL', 'REVERSE', 'CLEAR']
	        	},
	        	filterable: true,
	        	height: 'auto',
	        	on: function(data){
	        		//console.log(data)
	        		/* if(data.isAdd) {
		        		var arr = data.change;
		        		for(var k=0;k<arr.length;k++) {
		        			console.log(arr[k].__node.parent.name)
		        			arr[k].name = arr[k].__node.parent.name +'/'+ arr[k].name
		        		}
		        		//return data;
	        		}  */
	        	},
	        	data:ajaxdata
	        })
        })
        
        //服务保障
        var xmSelectInstanceService ;
         $.get('${base}${adminPrefix}/mall-pservice/xmSelectData',{skuId:'${mallPSku.id}'},function(ajaxdata){
        	//console.log(ajaxdata)
        	xmSelectInstanceService = xmSelect.render({
	        	el: '#serviceName', 
	        	autoRow: true,
	        	filterable: true,
	        	tree: {
	        		show: false,
	        		showFolderIcon: true,
	        		showLine: true,
	        		indent: 20,
	        		expandedKeys: [ -3 ],
	        	},
	        	toolbar: {
	        		show: true,
	        		list: ['ALL', 'REVERSE', 'CLEAR']
	        	},
	        	filterable: true,
	        	height: 'auto',
	        	on: function(data){
	        		//console.log(data)
	        	},
	        	data:ajaxdata
	        })
        })
        
        //选择货品
        layui.tableSelect.render({
    		elem: '#spuName',
    		checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
    		searchKey: 's_name',	//搜索输入框的name值 默认keyword
    		searchPlaceholder: '搜索货品名称',	//搜索输入框的提示文字 默认关键词搜索
    		table: {
    			url: '${base}${adminPrefix}/mall-pspu/list',
    			method:'post',
    			cols: [[
    				{ type: 'radio' },
    				{ field: 'id', title: 'ID',hide:true },
    				{ field: 'cataname', title: '分类',width:'20%' },
    				{ field: 'spuno', title: 'SPU编码',width:'15%'  },
    				{ field: 'name', title: '货品名称' },
    			]]
    		},
    		done: function (elem, data) {
    			//console.log(data)
    			var NEWJSON = []
    			var ids = []
    			layui.each(data.data, function (index, item) {
    				NEWJSON.push(item.name)
    				ids.push(item.id)
    			})
    			elem.val(NEWJSON.join(","))
    			$('#spuId').val(ids.join(","));
    		}
    	})
    	
    	//主图上传
        layui.upload.render({
            elem: '#test_thumbnail',
            url: '${base}/file/upload/',
            field:'test',
            before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo_thumbnail').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //如果上传失败
                if(res.success == false){
                    return layer.msg('上传失败');
                }
                $("#thumbnail").val(res.data.url);
            }
        });
    	
       //介绍上传
       var curUploadImgLen = 0;
       var uploadLoadIndex = null;
        layui.upload.render({
          elem: '#test_imgs'
          ,url: '${base}/file/upload/'
          ,field:'test'
          ,multiple: true
          ,before: function(obj){
        	  var len = $('#demo2').children().length;
          	console.log('已上传图片：'+len);
          	uploadLoadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
            	curUploadImgLen++;
            });
          }
          ,done: function(res){
            //上传完毕
           // console.log('图片上传完成')
          //  console.log(res)
             $('#demo2').append('<span class="layui-upload-img-container">'+
              '<img  src="'+ res.data.url +'" alt="'+ res.data.name +'" class="layui-upload-img">'+
              '<i class="layui-icon layui-upload-img-del" onclick="javascript:layui.jquery(this).parent().remove();">&#x1007;</i>'+
              '<span>')
              curUploadImgLen--;
              if(curUploadImgLen==0) {
            	  layer.close(uploadLoadIndex);
              }
          }
        });
        
        //初始化介绍图片
        var imgsJson = '${mallPSku.imgs}';
        if(imgsJson!='' && imgsJson!='null') {
        	var imgs = JSON.parse(imgsJson);
        	for(var k=0;k<imgs.length;k++) {
        		 $('#demo2').append('<span class="layui-upload-img-container">'+
                         '<img  src="'+ imgs[k] +'" class="layui-upload-img">'+
                         '<i class="layui-icon layui-upload-img-del" onclick="javascript:layui.jquery(this).parent().remove();">&#x1007;</i><span>')
        	}
        }

        form.on("submit(addMallPSku)",function(data){
        	//推荐
			data.field.isrec = (data.field.isrec=='on'?true:false);
			
			//判断主图
			if($('#thumbnail').val()==''){layer.msg('请上传主图');return false}
			
			//介绍图
			var imgsArr = [];
			$('#demo2').children().children('img').each(function(){
				var src = $(this).attr('src');
				imgsArr.push(src);
			});
			if(imgsArr.length==0) { layer.msg('请至少上传一张介绍图');return false}
			data.field.imgs = JSON.stringify(imgsArr);
			
			//选中的属性,后台用attrItemIds接收
			data.field.attrItemIds = xmSelectInstanceGoods.getValue('valueStr');
			//选中的属性,后台用attrItemIds接收
			data.field.serviceIds = xmSelectInstanceService.getValue('valueStr');
			
			delete data.field.select;//xmSelect默认input选中值 ，去掉
			if(data.field.attrItemIds==null||data.field.attrItemIds=='') {layer.msg('请至少选择一个属性');return false}
			
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            //给角色赋值
            $.post("${base}${adminPrefix}/mall-psku/edit",data.field,function(res){
                layer.close(loadIndex);
                if(res.success){
                    parent.layer.msg("编辑成功！",{time:1000},function(){
                    	//刷新父页面
						parent.layui.table.reload('test');
						parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }else{
                    layer.msg(res.message);
                }
            });
            return false;
        });

    });
</script>
</body>
</html>